package com.czl.module_service.fragment

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.PaymentData
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.HousesPaymentListAdapter
import com.czl.module_service.databinding.FragmentHousesPaymentListBinding
import com.czl.module_service.databinding.FragmentHousesPaymentListHeaderBinding
import com.czl.module_service.viewmodel.HousesPaymentListViewModel
import com.google.gson.JsonArray

@Route(path = AppConstants.Router.Service.F_HOUSES_PAYMENT_LIST_FRAGMENT)
class HousesPaymentListFragment : BaseFragment<FragmentHousesPaymentListBinding, HousesPaymentListViewModel>() {
    private lateinit var mAdapter: HousesPaymentListAdapter

    private var loaded: Boolean = false

    override fun initContentView(): Int {
        return R.layout.fragment_houses_payment_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        initAdapter()
        viewModel.tabIndex.set(arguments?.getSerializable(AppConstants.BundleKey.PAY_TAB_INDEX) as Int)

    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    private fun loadData() {
        if (!loaded) {
            reload()
        }
    }

    private fun initAdapter(){
        mAdapter = HousesPaymentListAdapter(this).apply {
            val headerBinding = FragmentHousesPaymentListHeaderBinding.inflate(layoutInflater)
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
        }
        binding.ryCommon.apply{
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            if (data != null) {
                viewModel.totalAmount.set(String.format("%.2f", data.totalAmount))
                mAdapter.setList(data.baseRoomInfoVOList)
            }
            binding.smartCommon.finishRefresh()
        })
        LiveBusCenter.observePayResultEvent(this, {
            when (it.payType) {
                AppConstants.Constants.PAY_SUCCESS_TYPE -> {
                    if (viewModel.tabIndex.get() == 1) { reload() }
                    Handler(Looper.myLooper()!!).postDelayed(object : Runnable{
                        override fun run() {
                            reload()
                        }
                    }, 1000)
                }
                AppConstants.Constants.PAY_FAIL_TYPE -> {
                    back()
                }
            }
        })
        viewModel.uc.loadFinallyEvent.observe(this, {
            loaded = true
        })
    }

    fun goToPay(bean : PaymentData) {
        /*val billIds = JsonArray()
        billIds.add(bean.billId)*/
        val payInfo = GsonUtils.toJson(
            mapOf(
                "billId" to bean.billId.toString(),
                "payMethod" to "chdZFB",
                "ownerId" to viewModel.model.getUserId().toString(),
                "phone" to viewModel.model.getLoginPhone(),
                "areaId" to viewModel.model.getAreaId(),
                "payAmount" to bean.payableMoney.toString(),
                "factRegister" to bean.payableMoney.toString(),
                "balaState" to ""
            )
        )
        Log.e("------->", payInfo.toString())
        startContainerActivity(AppConstants.Router.Pay.F_PAY_DETAIL, Bundle().apply {
            putString(AppConstants.BundleKey.PAY_ORDER_KEY, payInfo)
            putString(
                AppConstants.BundleKey.HOUSE_ORDER_IS,
                AppConstants.Constants.IS_HOUSE_ORDER
            )
        })
    }

    override fun reload() {
        viewModel.getRoomIdList()
    }
    override fun useBaseLayout(): Boolean {
        return false
    }
}